#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "cmd.h"

void exec_cmd(struct cmd *cmd)
{
    if (cmd == NULL) {
        perror("Invalid command structure");
        exit(0);
    }
    if (execvp(cmd->argv[0], cmd->argv) == -1) {
        perror("execvp");
        exit(0);
                                                                                    }
}

int builtin_cmd(struct cmd *cmd)
{
    if (strcmp(cmd->argv[0], "cd") == 0) {
        if (cmd->argc < 2) {
            printf("cd: missing operand\n");
            return 0;
        }
        if (chdir(cmd->argv[1]) == -1) {  
            perror("chdir");  
            return 0;  
        }
        return 1;
    } else if(strcmp(cmd->argv[0], "pwd") == 0) { 
        char cwd[1024];  
        if (getcwd(cwd, sizeof(cwd)) != NULL) {  
            printf("%s\n", cwd);  
            return 1;
        } else {  
            perror("getcwd");
            return 0; 
        }
    } else if (strcmp(cmd->argv[0], "exit") == 0) {  
        exit(0);  
        return 0;
    }
    return 0;
}
